//
//  FriendDetailViewController.m
//  FootPrint
//
//  Created by alex tao on 4/7/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "FriendDetailViewController.h"
#import "EventDetailViewController.h"
#import "MsgManager.h"

@interface FriendDetailViewController ()

@end

@implementation FriendDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _parser = [[FriendParser alloc]init];
        _parser.handleDelegate = self;
        _parser.delegate = self;
        _parser.addDelegate = self;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [_btnAddFriend setHidden:YES];
    
    UIImage* imgNormal =[[UIImage imageNamed:@"btn_bg_normal.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
    UIImage* imgPress =[[UIImage imageNamed:@"btn_bg_press.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];
    
    
    [_btnAddFriend setBackgroundImage:imgNormal forState:UIControlStateNormal];
    [_btnAddFriend setBackgroundImage:imgPress forState:UIControlStateHighlighted];

    [_btnDegree setBackgroundImage:imgNormal forState:UIControlStateNormal];
    [_btnDegree setBackgroundImage:imgPress forState:UIControlStateHighlighted];
    
    [_btnDisagree setBackgroundImage:imgNormal forState:UIControlStateNormal];
    [_btnDisagree setBackgroundImage:imgPress forState:UIControlStateHighlighted];
    
//    if(_isCheck){
//        if([[FriendManager sharedInstance] isLatestList]){
//             self.allFriends = [[FriendManager sharedInstance] allFriends];
//            [self checkIsFriend];
//        } else {
//            [_parser updateFriendListForUid:[GConfig getUserId]];
//        }
//    }
    
}

- (void)doCheck{
    if([[FriendManager sharedInstance] isLatestList]){
        self.allFriends = [[FriendManager sharedInstance] allFriends];
        [self checkIsFriend];
    } else {
        [_parser updateFriendListForUid:[GConfig getUserId]];
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_user release];
    [_iconImage release];
    [_accountLabel release];
    [_sigLabel release];
    [_backupLabel release];
    [_activityBtn release];
    [_btnDegree release];
    [_btnDisagree release];
    [_btnAddFriend release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setIconImage:nil];
    [self setAccountLabel:nil];
    [self setSigLabel:nil];
    [self setBackupLabel:nil];
    [self setActivityBtn:nil];
    [self setBtnDegree:nil];
    [self setBtnDisagree:nil];
    [self setBtnAddFriend:nil];
    [super viewDidUnload];
}

- (IBAction)back {
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self.navigationController dismissModalViewControllerAnimated:YES];
    }
}

- (IBAction)activity
{
    EventDetailViewController * detail = [[[EventDetailViewController alloc] initWithNibName:@"EventDetailViewController" bundle:nil] autorelease];
    
    UINavigationController * navVc = self.navigationController;
    if (nil == navVc) {
        navVc = [[[UINavigationController alloc] initWithRootViewController:detail] autorelease];
        navVc.navigationBarHidden = YES;
        [ROOT_VC presentModalViewController:navVc animated:YES];
    } else {
        [navVc pushViewController:detail animated:YES];
    }
    
    [detail createWithFriendList:[NSArray arrayWithObject:_user]];
}

- (IBAction)actionAgree:(id)sender {
    
    [_parser handInvite:[GConfig getUserId] inviteUserId:[_user id] state:0];
    _isAgree = YES;
    
    if(_rawData != nil){
        [[MsgManager sharedInstance] removeMsg:_rawData];
    } else if (_user) {
        [[MsgManager sharedInstance] removeMsg:[MsgManager makeInviteMsg:_user.id]];
    }
}

- (IBAction)actionDisagree:(id)sender {
    [_parser handInvite:[GConfig getUserId] inviteUserId:[_user id] state:1];
    _isAgree = NO;
    
    if(_rawData != nil){
        [[MsgManager sharedInstance] removeMsg:_rawData];
    } else if (_user) {
        [[MsgManager sharedInstance] removeMsg:[MsgManager makeInviteMsg:_user.id]];
    }
}

- (IBAction)addFriend:(id)sender {
    [_parser addFriendByUid:_user.id myId:[GConfig getUserId]];
}

- (void)updateUserStatus:(UserMessage*)status
{
    [_user release];
    _user = [status retain];
    
    if (!status.portrait.isSys) {
        [_iconImage loadAsyncImage:status.portrait.imageUrl];
    } else {
        [_iconImage setSysImageWithId:status.portrait.id];
    }
    
    _accountLabel.text = status.nickName ? [NSString stringWithFormat:@"账号：%@", status.nickName] : @"账号：";
    _sigLabel.text = status.signName ? [NSString stringWithFormat:@"签名：%@", status.signName] : @"签名：";
}

- (void)didFinishHandle:(FriendInvitationProcessResponseMessage*)result{

    if(result != nil && [result resultCode]==0){
    
        [[NSNotificationCenter defaultCenter]postNotificationName:NOTIFY_HANDLE_INVITE object:nil];
        if(_isAgree){
            
            [[NSNotificationCenter defaultCenter]postNotificationName:NOTIFY_REFRESH_FRIEND object:nil];
        }
        
        [[[iToast makeText:@"已处理"]setDuration:2000]show];
        if (self.navigationController.viewControllers.count > 1) {
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [self.navigationController dismissModalViewControllerAnimated:YES];
        }
        return;
    }
    [[[iToast makeText:@"服务器出错"]setDuration:2000]show];
}
- (void)failHandle{
    [[[iToast makeText:@"服务器出错"]setDuration:2000]show];
}

- (void) didRecieveFriendList:(FriendQueryResponseMessage*)friends{

    if (friends && friends.resultCode == 0) {
        self.allFriends = [GToolUtil PBArr2NSArr:friends.friendList];
        [self checkIsFriend];
    }
}
- (void) didRecieveGroupList:(GroupQueryResponseMessage*)friends{

}
- (void) didDeleteGroup:(GroupDeleteResponseMessage*)result{

}

- (void) checkIsFriend{
    
    if(_user.id == [GConfig getUserId]){
        return;
    }
    BOOL isShowAdd = YES;
    for(int i=0; i<_allFriends.count; i++){
        FriendQueryResponseMessage_FriendList * msg = [_allFriends objectAtIndex:i];
        if(_user.id == msg.user.id){
            isShowAdd = NO;
            break;
        }
    }
    [_btnAddFriend setHidden: !isShowAdd];
}

- (void)didAddByPhone:(PhoneNoCheckResponseMessage*)result{

}
- (void)failAddByPhone{

}

- (void)didAddByUid:(FriendAddResponseMessage*)result{
    if(result != nil && [result resultCode]==0){
         [[[iToast makeText:@"已发送请求"]setDuration:2000]show];
    } else {
        [[[iToast makeText:@"服务器出错"]setDuration:2000]show];
    }
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self.navigationController dismissModalViewControllerAnimated:YES];
    }
}
- (void)failAddByUid{
     [[[iToast makeText:@"服务器出错"]setDuration:2000]show];
    if (self.navigationController.viewControllers.count > 1) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self.navigationController dismissModalViewControllerAnimated:YES];
    }
}

@end
